package com.lisynet.tools.mytools.controller;


import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.lisynet.tools.mytools.annotation.MasterRouteOnly;
import com.lisynet.tools.mytools.entity.User;
import com.lisynet.tools.mytools.service.CacheService;
import com.lisynet.tools.mytools.service.IUserService;
//import org.apache.shardingsphere.api.hint.HintManager;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lisy
 * @since 2022-01-12
 */
@RestController
@RequestMapping("/user")
public class UserController {
    /**
     * mybatis(mybatis plus)
     */
    @Resource
    private IUserService userService;
    @Resource
    private CacheService cacheService;
    /**
     * jdbc
     */
    @Resource
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;


    /*********************  mybatis(mybatis plus)  ************************/
    /**
     * 添加事务后自动走主库
     */
    @RequestMapping("addInfo")
    public User addUser(){
        User info = User.builder().name(IdUtil.simpleUUID()).age(22).build();
        return userService.addUser(info);
    }
    /**
     * 查询走从库
     */
    @RequestMapping("getInfo")
    public User getUser(Integer id) throws Exception {
        if(id==null){
            throw new Exception("参数错误");
        }
//        cacheService.set(IdUtil.simpleUUID(),IdUtil.simpleUUID());
//        System.out.println(cacheService.get("0338b5c611b2424683234d6071828fea"));
        return userService.getInfo(id);
    }

    /**
     * 添加事务后自动走主库
     */
    @RequestMapping("updateInfo")
    public User updateInfo(Integer id) throws Exception {
        if(id==null){
            throw new Exception("参数错误");
        }
        User u = userService.changeInfo(id);
        return u;
    }


    /*********************  jdbc  ************************/
    /**
     * 增加更新走主库，查询走从库
     */
    @RequestMapping("jdbcAddInfo")
    public User jdbcAddInfo(){
        User userInfo = fun();
        return userInfo;
    }
    private User fun(){
        //插入
        String sql1 = "insert user(name,age) values(:name,:age)";
        Map<String,Object> params1 = new HashMap<>(2);
        String name = IdUtil.simpleUUID();
        params1.put("name",name);
        params1.put("age",20);
        namedParameterJdbcTemplate.update(sql1,params1);
//        HintManager hintManager = HintManager.getInstance();
//        hintManager.setMasterRouteOnly();
        //查询（查询强制走主库）
        String sql3 = "select * from user where name=:name";
        Map<String,Object> params3 = new HashMap<>(1);
        params3.put("name",name);
        RowMapper<User> rowMapper=new BeanPropertyRowMapper<>(User.class);
        User userInfo = namedParameterJdbcTemplate.queryForObject(sql3, params3, rowMapper);
//        hintManager.close();
        return userInfo;
    }

    /**
     * 使用注解强制走主库
     */
    @RequestMapping("jdbcGetInfo")
    @MasterRouteOnly
    public User jdbcGetInfo(){
        String sql3 = "select * from user where name=:name";
        Map<String,Object> params3 = new HashMap<>(1);
        params3.put("name","e664246ae3ef480e8a7e0b97588c07eb");
        RowMapper<User> rowMapper=new BeanPropertyRowMapper<>(User.class);
        User userInfo = namedParameterJdbcTemplate.queryForObject(sql3, params3, rowMapper);
        return userInfo;
    }
    /**
     * redis
     */
    @RequestMapping("/setRedisInfo")
    public String setRedisInfo(String key,String value){
        if(StrUtil.isBlank(key)){
            key = IdUtil.simpleUUID();
            value = IdUtil.simpleUUID();
        }
        cacheService.set(key,value);
        return cacheService.get(key).toString();
    }
    /**
     * redis
     */
    @RequestMapping("/getRedisInfo")
    public String getRedisInfo(String key){
        return cacheService.get(key).toString();
    }
}
